package command
{
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.net.URLRequestMethod;
	
	import model.ApplicationModel;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;
	import vo.Level;
	
	public class LevelsManagerCommand extends BaseCommand
	{
		private var _model:ApplicationModel=ApplicationModel.instance;
		public override function doExecute(event:CairngormEvent):void
		{
			// Test parametros 
			var params:Object = new Object();
			var httpService:HTTPService = new HTTPService();
			httpService.url= _model.URL_LEVEL;
			httpService.useProxy = false;
			httpService.addEventListener(ResultEvent.RESULT,onResult);
          	httpService.addEventListener(FaultEvent.FAULT, onFault);
          	httpService.method = URLRequestMethod.POST;
          	httpService.send(params);	
          	
		}

		public function onResult(data:Object):void
		{
			
			var resultObj:Object = data.result;
			var i:int=0;
			_model.levels = new ArrayCollection();
			while (resultObj.data.level.length > i )
			{
				var level:Level = new Level();
				level.label =  resultObj.data.level[i].label;
				level.id =   resultObj.data.level[i].id;
				_model.levels.addItem(level);
				i++;
			}
		}	
		
		public function onFault(data:Object):void
		{
			Alert.show("Fail","Fail");
		}

	}
}